package com.hjq.mallware.service.impl;

import com.alibaba.fastjson.JSON;
import com.hjq.common.utils.R;
import com.hjq.mallware.entity.vo.MemberRecieveEntity;
import com.hjq.mallware.feign.memberFeign;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallware.dao.WareInfoDao;
import com.hjq.mallware.entity.WareInfoEntity;
import com.hjq.mallware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
@Autowired
    memberFeign memberFeign;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key= (String) params.get("key");
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty("key")){
            wrapper.eq("id",key).or().like("address",key).or().like("name",key).or().eq("areacode",key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public BigDecimal getFare(Long addrId) {
        MemberRecieveEntity memberRecieveEntity = getMemberRecieveEntity(addrId);
        //memberRecieveEntity.get
        BigDecimal fare = new BigDecimal(0);
        if(memberRecieveEntity!=null){
            String phone = memberRecieveEntity.getPhone();
            fare=new BigDecimal(phone.substring(phone.length()-1));
        }
        return fare;
    }
    @Override
    public MemberRecieveEntity getMemberRecieveEntity(Long addrId) {
        R info = memberFeign.info(addrId);
        Object data = info.get("memberReceiveAddress");
        String string = JSON.toJSONString(data);
        return JSON.parseObject(string, MemberRecieveEntity.class);
    }

}